<?php

/*
 * @Author: Shawn Zhang 
 * @Date: 2019-08-22 18:17:43 
 * @Last Modified by: Shawn Zhang
 * @Last Modified time: 2019-08-22 19:20:56
 * @used-for 操作App用户的肤质检测记录
 */

namespace App\Http\Controllers\Admin\V1;

use App\Exceptions\ApiErrCode;
use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Utils\SkinRunSkinDetect;
// use App\Services\Utils\OSSable;
use App\Models\SkinRecords;
use App\Models\SkinRunRecords;
use Illuminate\Support\Carbon;
use App\Models\User;

class UserSkinRecordController extends ApiController
{

    private $skinRunDetector;
    
    public function __construct()
    {
        $token =\env('SKINRUN_TOKEN');
        $version =\env('SKINRUN_VERSION');
        $url =\env('SKINRUN_URL');
        $this->skinRunDetector = new SkinRunSkinDetect($token, $version, $url);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $user_id)
    {
        $code=ApiErrCode::API_CODE_OK;
        $error = "操作成功";
        $data=null;

        $user = User::find($user_id);
        
        if(!isset($user))
        {
            $code = ApiErrCode::REQUEST_DATA_ERROR;
            $error="该用户不存在";
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $star_time = $request->get('start_time');
        $end_time = $request->get('end_time');
        if($star_time==null || $star_time=="")
            $star_time=Carbon::create('1970-01-01 00:00:00')
                                ->setTimezone('utc')
                                ->toDateTimeString();
        if($end_time==null || $end_time=="")
            $end_time=Carbon::now()
                            ->setTimezone('utc')
                            ->toDateTimeString();
        
        // 分页的话每页的数据
        $skinRecordColls=$user->userSkinRecords();
        $perPage = $request->per_page ?? \count($skinRecordColls->get());
        if((int)$perPage <=0)
            $perPage=\count($skinRecordColls->get());
        $page = $request->page ?? 1;
        if((int)$page <=0)
            $page=1;

        $paginator = $skinRecordColls->whereBetween('created_at',[$star_time,$end_time])
                                     ->latest('created_at')
                                     ->paginate($perPage);
        $data=[];
        $detecor; // 肤质检测方法
        foreach($paginator->items() as $item)
        {
            $item_detail;
            switch($item->record_type)
            {
                case "skinrun":
                    $item_detail=SkinRunRecords::where('id',$item->record_id)->first();
                    $detecor=$this->skinRunDetector;
                    break;
            }
            $data[]=$detecor->analysisDetectResult($item, $item_detail, 'zh-CN');
        } 
        $respone = new \stdClass();
        $respone->current_page=$paginator->currentPage();
        $respone->last_page=$paginator->lastPage();
        $respone->data=$data;
        $respone->total=$paginator->total();

        return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$respone]);
    }

    /**
     * 获取指定id的肤质记录
     */
    public function showRecord(Request $request, $id)
    {
        $code=ApiErrCode::API_CODE_OK;
        $error="操作成功";
        $data=null;

        $skinrecord=SkinRecords::find($id);
        if(!isset($skinrecord))
        {
            $code = ApiErrCode::REQUEST_DATA_ERROR;
            $error="该肤质记录id不存在";
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $detecor=null;
        $record_detail=null;
        switch($skinrecord->record_type)
        {
            case 'skinrun':
                $detecor=$this->skinRunDetector;
                $record_detail=SkinRunRecords::where('id',$skinrecord->record_id)->first();
                break;
        }
        
        $data=$detecor->analysisDetectResult($skinrecord, $record_detail, 'zh-CN');
        return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);


    }

    /**
     * 删除制定id的测服记录
     */
    public function destroy(Request $request)
    {
        $code=ApiErrCode::API_CODE_OK;
        $error="操作成功";

        $skinrecord=SkinRecords::find($request->record_id);
        if(!isset($skinrecord))
        {
            $code = ApiErrCode::REQUEST_DATA_ERROR;
            $error="该肤质记录id不存在";
            return json_encode(['code'=>$code, 'error'=>$error]);
        }
        $record_detail=null;
        switch($skinrecord->record_type)
        {
            case 'skinrun':
                $record_detail=SkinRunRecords::where('id',$skinrecord->record_id)->first();
                break;
        }
        $record_detail->delete();
        $skinrecord->delete();
        
        return json_encode(['code'=>$code, 'error'=>$error]);
    }
}
